/*
  Example 20.2
 ST Microelectronics CN75 Digital Temperature sensor demonstration sketch
 Farnell part number 1737226; RS part number 445-579
 http://tronixstuff.com/tutorials > chapter 20
 CC by-sa v3.0
 */

#include "Wire.h"
#define cn75address 0x48 // with pins 5~7 set to GND, the device address is 0x48

void setup()
{
  Wire.begin(); // wake up I2C bus
  Serial.begin(9600);
}

void getCN75data(byte *a, byte *b)
{
  // move the register pointer back to the first register
  Wire.beginTransmission(cn75address); // "Hey, CN75 @ 0x48! Message for you"
  Wire.send(0);        // "move your register pointer back to 00h"
  Wire.endTransmission(); // "Thanks, goodbye..."

  // now get the data from the CN75
  Wire.requestFrom(cn75address, 2); // "Hey, CN75 @ 0x48 - please send me the contents of your first two registers"
  *a = Wire.receive(); // first received byte stored here
  *b = Wire.receive(); // second received byte stored here
}

void showCN75data()
{
  byte aa,bb;
  float temperature=0;
  getCN75data(&aa,&bb);



  if (aa>127) // check for below zero degrees
  {
    temperature=(128-(128-aa));
    if (bb==128)    // check for 0.5 fraction
    {
      temperature-=0.5;
    }
  } 
  else // it must be above zero degrees
  {
    temperature=aa;
    if (bb==128)   // check for 0.5 fraction
    {
      temperature+=0.5;
    }
  }










  Serial.print("Temperature = ");
  Serial.print(temperature,1);
  Serial.println(" degrees C");
  delay(1000);
}

void loop()
{
  showCN75data();
}

